//
//  ProductionViewController.m
//  GameBoss
//
//  Created by 张艺 on 15/4/30.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "ProductionViewController.h"


#define ScreenSize [UIScreen mainScreen].bounds.size

@interface ProductionViewController ()
// 设置一个scroller来放概述
@property (nonatomic, retain) UIScrollView *rootScrollView;
// 显示详细参数
@property (nonatomic, retain) UIScrollView *dataScrollView;

// 设置一个segment
@property (nonatomic, retain) UISegmentedControl *DataSegment;

@end

@implementation ProductionViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

//        self.model.items = [NSMutableArray array];
    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 设置segment
    self.DataSegment = [[UISegmentedControl alloc] initWithItems:@[@"概述",@"参数"]];
    self.DataSegment.frame = CGRectMake(0, 0, ScreenSize.width, ScreenSize.height * 25 / 375);
    [self.view addSubview:self.DataSegment];
    [self.DataSegment addTarget:self action:@selector(click) forControlEvents:UIControlEventValueChanged];
    self.DataSegment.selectedSegmentIndex = 0;
    [_DataSegment release];
    
    // 概述scroller
    self.rootScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, ScreenSize.height * 25 / 375, ScreenSize.width, ScreenSize.height)];
    [self.view addSubview:self.rootScrollView];
    self.rootScrollView.tag = 2001;
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenSize.width, ScreenSize.height)];
    imageView.image = [UIImage imageNamed:@"zaochen"];
    [self.rootScrollView addSubview:imageView];
    [imageView release];
    [self creatScrollerInformation];
    [_rootScrollView release];
    
    // 详细参数
    self.dataScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, ScreenSize.height * 25 / 375, ScreenSize.width, ScreenSize.height - Screen.height * 25 / 375 - 113)];
    [self.view addSubview:self.dataScrollView];
    self.dataScrollView.tag = 2002;
    UIImageView *imageViewSec = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenSize.width, ScreenSize.height)];
    imageViewSec.image = [UIImage imageNamed:@"zaochen"];
    [self.dataScrollView addSubview:imageViewSec];
    self.dataScrollView.contentSize = CGSizeMake(Screen.width, (self.model.items.count / 2) * 60  * ScreenSize.height / 667);
    self.dataScrollView.delegate = self;
    [imageViewSec release];
    [self creatScrollerData];
    [_dataScrollView release];
    
    [self.view bringSubviewToFront:self.rootScrollView];
    
    self.dataScrollView.hidden = YES;
    
}

- (void)creatScrollerInformation
{
    // 图片
    UIImageView *picImageView = [[UIImageView alloc] initWithFrame:CGRectMake(70 * Screen.width / 375, Screen.height * 40/667, 230 * Screen.width / 375, 190 * Screen.height / 667)];
    [self.rootScrollView addSubview:picImageView];
    [picImageView sd_setImageWithURL:[NSURL URLWithString:self.model.idxPic]];
    
    // title
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 250 * Screen.height / 667, Screen.width, ScreenSize.height * 30 / 375)];
    [self.rootScrollView addSubview:titleLabel];
    titleLabel.text = self.model.shortName;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont systemFontOfSize:18];
    [titleLabel release];
    
    // 价格
    UILabel *priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 300 * Screen.height / 667, ScreenSize.width, ScreenSize.height * 30 /375)];
    [self.rootScrollView addSubview:priceLabel];
    
    if (self.model.price <= 0) {
        priceLabel.text = @"暂无报价";
    }
    else{
        NSString *price = [NSString stringWithFormat:@"官方报价: %ld 元", self.model.price];
        priceLabel.text = price;
    }
    priceLabel.textAlignment = NSTextAlignmentCenter;
    priceLabel.font = [UIFont systemFontOfSize:18];
    priceLabel.textColor = [UIColor redColor];
    
//    [self reloadInputViews];
    [priceLabel release];
}

- (void)creatScrollerData
{
    if (![self.model.items[0] isEqualToString:@""] ) {
        
    for (int i = 0; i < self.model.items.count / 2; i++) {
        for (int j = 0;  j < 2; j++) {
            UILabel *label = [self creatLabel:CGRectMake(j * Screen.width / 3 + 50 * ScreenSize.width / 667,
                                                         60 * ScreenSize.height / 667 * i + 30 * Screen.height / 667,
                                                         Screen.width / 2 + 50 * ScreenSize.width / 667,
                                                         60  * ScreenSize.height / 667 )];
            label.text = self.model.items[i * 2 + j];
            label.font = [UIFont systemFontOfSize:12];
            label.textAlignment = NSTextAlignmentCenter;
            label.numberOfLines = 0;
            [label sizeToFit];
        }
        
    }
    }
    else
    {
        UILabel *label = [self creatLabel:CGRectMake(0, 0, Screen.width, 200 * Screen.height / 667)];
        label.text = @"暂无详细参数";
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:18];
        label.textColor = [UIColor grayColor];
    }
}

- (void)click
{
//    if (self.DataSegment.selectedSegmentIndex == 1)
    
    if (self.DataSegment.selectedSegmentIndex == 0){
        self.rootScrollView.hidden = NO;
        self.dataScrollView.hidden = YES;
    }
    else{
        self.rootScrollView.hidden = YES;
        self.dataScrollView.hidden = NO;
    }

}

- (UILabel *)creatLabel:(CGRect)frame
{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    [self.dataScrollView addSubview:label];
    [label release];
    return label;
}

//#pragma mark 选取白黑两种模式
//- (void)setNightDayModel {
//    [NightManager setBackgroundColorWithView:self.rootScrollView];
//    [NightManager setBackgroundColorWithView:self.dataScrollView];
//}
//
//- (void)setLightDayModel {
//    [NightManager setBackgroundColorWithView:self.rootScrollView];
//    [NightManager setBackgroundColorWithView:self.dataScrollView];
//}

    
- (void)dealloc
{
    [_model release];
    [_rootScrollView release];
    [_dataScrollView release];
    [_DataSegment release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
